<<<<<<< .mine
# Are any of these rare?
import httplib
import urllib2
from urlparse import *
import atom.service
import urllib
import cookielib
import os # dont like how it needs this...

try:
    import gdata.docs.service
except(ImportError):
    installGoogleDocs()

class ProxyHTTPConnection(httplib.HTTPConnection):

        _ports = {'http' : 80, 'https' : 443}

        def request(self, method, url, body=None, headers={}):
                #request is called before connect, so can interpret url and get
                #real host/port to be used to make CONNECT request to proxy
                proto, rest = urllib.splittype(url)
       
                if proto is None:
                        raise ValueError, 'unknown URL type: %s' % url
                #get host
                host, rest = urllib.splithost(rest)
                #try to get port
                host, port = urllib.splitport(host)
                #if port is not defined try to get from proto
                if port is None:
                        try:
                                port = self._ports[proto]
                        except KeyError:
                                raise ValueError, 'unknown protocol for: %s' % url
                self._real_host = host
                self._real_port = port
                httplib.HTTPConnection.request(self, method, url, body, headers)

        def connect(self):
                httplib.HTTPConnection.connect(self)
       
                # Modified by Devraj Mukherjee to add authentication support basic auth
                #send proxy CONNECT request
                extra_string = 'CONNECT %s:%s HTTP/1.0\r\n' % (self._real_host, self._real_port)
                extra_string += self._get_authentication_string() + '\r\n'
                self.send(extra_string)

                #expect a HTTP/1.0 200 Connection established
                response = self.response_class(self.sock, strict=self.strict, method=self._method)
                (version, code, message) = response._read_status()
                #probably here we can handle auth requests...
                if code != 200:
                        #proxy returned and error, abort connection, and raise exception
                        self.close()
                        raise socket.error, 'Proxy connection failed: %d %s' % (code, message.strip())
                #eat up header block from proxy....
                while True:
                        #should not use directly fp probablu
                        line = response.fp.readline()
                        if line == '\r\n': break
               
        def _get_authentication_string(self):
                auth_string = ''
                proxy_username = os.environ.get('proxy-username')
                proxy_password = os.environ.get('proxy-password')
                if proxy_username and proxy_password:
                        encoded_user_pass = base64.encodestring('%s:%s' % (proxy_username, proxy_password))
                        auth_string = 'Proxy-authorization: Basic %s\r\n' % encoded_user_pass
               
                return auth_string
class ProxyHTTPSConnection(ProxyHTTPConnection):
       
        default_port = 443

        def __init__(self, host, port = None, key_file = None, cert_file = None, strict = None):
                ProxyHTTPConnection.__init__(self, host, port)
                self.key_file = key_file
                self.cert_file = cert_file
        def connect(self):
                ProxyHTTPConnection.connect(self)
                #make the sock ssl-aware
                ssl = socket.ssl(self.sock, self.key_file, self.cert_file)
                self.sock = httplib.FakeSocket(self.sock, ssl)
class ConnectHTTPHandler(urllib2.HTTPHandler):


        def __init__(self, proxy=None, debuglevel=0):
                self.proxy = self._parse_proxy_details(proxy)
                urllib2.HTTPHandler.__init__(self, debuglevel)

        def do_open(self, http_class, req):
                if self.proxy is not None:
                        req.set_proxy(self.proxy, 'http')
                return urllib2.HTTPHandler.do_open(self, ProxyHTTPConnection, req)

        def _parse_proxy_details(self, proxy_string):
                proto, rest = urllib.splittype(proxy_string)
                host,  rest = urllib.splithost(rest)
                host,  port = urllib.splitport(host)
                return '%s:%s' % (host, port)
class ConnectHTTPSHandler(urllib2.HTTPSHandler):

        def __init__(self, proxy=None, debuglevel=0):
                self.proxy = self._parse_proxy_details(proxy)
                urllib2.HTTPSHandler.__init__(self, debuglevel)

        def do_open(self, http_class, req):
                if self.proxy is not None:
                        req.set_proxy(self.proxy, 'https')
                return urllib2.HTTPSHandler.do_open(self, ProxyHTTPSConnection, req)
       
        def _parse_proxy_details(self, proxy_string):
                proto, rest = urllib.splittype(proxy_string)
                host,  rest = urllib.splithost(rest)
                host,  port = urllib.splitport(host)
                return '%s:%s' % (host, port)  

SPREADSHEET_FORMATS=['csv','tsv','ods','tab','xls']
PRESENTATION_FORMATS=['ppt','pps']
DOCUMENT_FORMATS=['doc','odt','rtf','sxw','txt','htm','html']
mimes={'csv': 'text/csv', 
       'tsv': 'text/tab-separated-values',
       'tab': 'text/tab-separated-values',
       'doc': 'application/msword',
       'ods': 'application/x-vnd.oasis.opendocument.spreadsheet',
       'odt': 'application/vnd.oasis.opendocument.text',
       'rtf': 'application/rtf',
       'sxw': 'application/vnd.sun.xml.writer',
       'txt': 'text/plain',
       'xls': 'application/vnd.ms-excel',
       'ppt': 'application/vnd.ms-powerpoint',
       'pps': 'application/vnd.ms-powerpoint',
       'htm': 'text/html',
       'html': 'text/html'}
DOCUMENT_QUERY='http://docs.google.com/MiscCommands?command=saveasdoc&docID=%s&exportformat=%s'
SPREADSHEET_QUERY='http://spreadsheets.google.com/pub?key=%s&output=%s'
PRESENTATION_QUERY='https://docs.google.com/MiscCommands?command=saveasdoc&exportFormat=%s&docID=%s'

def installGoogleDocs(self):
    import sys
    if sys.platform == 'linux2':
        import platform
        if platform.dist=='Ubuntu':
            import os
            os.system('gksu apt-get install python-gdata')
        else:
            print 'Please download and install http://gdata-python-client.googlecode.com/files/gdata.py-1.2.3.tar.gz'
    elif sys.platform == 'win32':
        print 'Please download and install http://gdata-python-client.googlecode.com/files/gdata.py-1.2.3.zip'
    elif sys.platform == 'mac':
        print 'Please download and install http://gdata-python-client.googlecode.com/files/gdata.py-1.2.3.zip'


class sink:
    name='Google Docs'
    files=[]
    time=0
    _cookie_jar = None
    _url_google_auth          = 'https://www.google.com/accounts/ServiceLoginAuth'
    _url_google_followup  = 'https://docs.google.com'
    _user_agent = 'Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.8.1.6) Gecko/20061201 Firefox/2.0.0.6 (Ubuntu-feisty)'
    def __init__(self,config):
        if config=={}:
            import os
            print "No information for Google Docs"
            config['username']=raw_input('Google Docs username: ')
            config['password']=raw_input('Google Docs password: ')
            config['document']='doc'
            config['presentation']='ppt'
            config['spreadsheet']='xls'
            
            os.environ['SYNC.GDOCS.USERNAME']=config['username']
            os.environ['SYNC.GDOCS.PASSWORD']=config['password']
            os.environ['SYNC.GDOCS.DOCUMENT']=config['document']
            os.environ['SYNC.GDOCS.PRESENTATION']=config['presentation']
            os.environ['SYNC.GDOCS.SPREADSHEET']=config['spreadsheet']                    
        print 'logging in to Google Docs'

        self._cookie_jar = cookielib.CookieJar()

        self.gd_client = gdata.docs.service.DocsService()
        self.gd_client.ClientLogin(config['username'],config['password'])
        
        login_data = {'PersistentCookies': 'true', 'Email': config['username'], 'Passwd': config['password'], 
                      'continue': self._url_google_followup, 'followup': self._url_google_followup}
        prepared_auth_url = self._url_google_auth
        response = self._open_https_url(prepared_auth_url, login_data)
        
        print 'logged in.'
        self.config=config
    def getContent(self,file):
        return self.get_file(file['id'],file['format'])
    def get_files(self):
        try:
            files=[]
            for entry in self.gd_client.GetDocumentListFeed().entry:
                folder='.'
                for category in entry.category:
                    if category.scheme.startswith('http://schemas.google.com/docs/2007/folders/'):
                        folder=entry.category[1].label
                        print entry.category[1].label

                file={'format': self.config[entry.category[0].label],
                            'id': self.get_document_id(entry.GetAlternateLink().href),
                            'author': {'name':entry.author[0].name.text,'email':entry.author[0].email.text},
                            #'updated': entry.updated.text,
                            #'published': entry.published.text,
                            'title': entry.title.text,
                            'edit-link': entry.GetEditLink().href,
                            'folder': folder}
                files.append(file)
            return files
        except(gdata.service.RequestError):
            print "Google Docs is not responding..."
            return self.get_files()
    def get_document_id(self, alternate_link):
        parsed_url = urlparse(alternate_link)
        url_params = parsed_url[4]
        document_id = url_params.split('=')[1]
        return document_id
    def get_file(self,id,format):
        if format in DOCUMENT_FORMATS or format in PRESENTATION_FORMATS:
            url=DOCUMENT_QUERY%(id,format)
        elif format in SPREADSHEET_FORMATS:
            url=SPREADSHEET_QUERY%(id,format)
        return self._open_https_url(url, post_data = None).read()
    def _open_https_url(self, target_url, post_data = None):
            # Opener will be assigned to either a proxy enabled or disabled opener
            opener    = None
            proxy_url = os.environ.get('http_proxy')
            
            if proxy_url:
                    opener = urllib2.build_opener(ConnectHTTPHandler(proxy = proxy_url),
                                                  ConnectHTTPSHandler(proxy = proxy_url),
                                                  urllib2.HTTPCookieProcessor(self._cookie_jar))
            else:
                    opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(self._cookie_jar))

            # Proxy or not, add the headers and place the POST reuqest
            opener.addheaders = [('User-agent', self._user_agent)]
            
            response = None
            if post_data:
                    response = opener.open(target_url, urllib.urlencode(post_data))
            else:
                    response = opener.open(target_url)
                    
            return response
    def create(self,file):
        try:
            ms = gdata.MediaSource(file['content'], mimes[file['format'].lower()], len(file['content']), '', file['title'])
        except(KeyError):
            print file['format']
            return
        
        media_entry = gdata.GDataEntry()
        media_entry.title = atom.Title(text=file['title'])

        if file['format'] in SPREADSHEET_FORMATS:
            media_entry.category.append(atom.Category(scheme=gdata.docs.service.DATA_KIND_SCHEME,
                                                      term=gdata.docs.service.SPREADSHEET_KIND_TERM))
        elif file['format'] in DOCUMENT_FORMATS:
            media_entry.category.append(atom.Category(scheme=gdata.docs.service.DATA_KIND_SCHEME,
                                    term=gdata.docs.service.DOCUMENT_KIND_TERM))
        elif file['format'] in PRESENTATION_FORMATS:
            media_entry.category.append(atom.Category(scheme=gdata.docs.service.DATA_KIND_SCHEME,
                                                      term=gdata.docs.service.PRESENTATION_KIND_TERM))
        #if file['starred']:
        #    if file['format'] in DOCUMENT_FORMATS:
        #        media_entry.category.append(atom.Category
        #                                (scheme='http://schemas.google.com/g/2005/labels',
        #                                 term='http://schemas.google.com/g/2005/labels#starred'))
        #    elif file['format'] in SPREADSHEET_FORMATS:
        #        media_entry.category.append(atom.Category
        #                                (scheme='http://schemas.google.com/docs/2007/folders/%s'%file['author']['email'],
        #                                 term='My Favorite Spreadsheets'))
        #if file['author']['name']:
        #    media_entry.author.name=file['author']['name']
        #if file['author']['email']:
        #    media_entry.author.email=file['author']['email']
        #if file['updated']:
        #    media_entry.id=file['updated']
        #if file['published']:
        #    media_entry.id=file['published']
        
        if file['folder']=='.':
            feed='/feeds/documents/private/full'
        #else:
        #    feed='/feeds/folders/private/full/folder%3A%s' % file['folder']
        else:
            feed='/feeds/documents/private/full'
            
        self.gd_client.Post(media_entry, feed,
                                media_source = ms,
                                extra_headers = {'Slug' : file['title'] + '.' + file['format'] })
    def remove(self,file):
        self.gd_client.Delete(file['edit-link'])=======
class ProxyHTTPConnection(httplib.HTTPConnection):

        _ports = {'http' : 80, 'https' : 443}

        def request(self, method, url, body=None, headers={}):
                #request is called before connect, so can interpret url and get
                #real host/port to be used to make CONNECT request to proxy
                proto, rest = urllib.splittype(url)
       
                if proto is None:
                        raise ValueError, 'unknown URL type: %s' % url
                #get host
                host, rest = urllib.splithost(rest)
                #try to get port
                host, port = urllib.splitport(host)
                #if port is not defined try to get from proto
                if port is None:
                        try:
                                port = self._ports[proto]
                        except KeyError:
                                raise ValueError, 'unknown protocol for: %s' % url
                self._real_host = host
                self._real_port = port
                httplib.HTTPConnection.request(self, method, url, body, headers)

        def connect(self):
                httplib.HTTPConnection.connect(self)
       
                # Modified by Devraj Mukherjee to add authentication support basic auth
                #send proxy CONNECT request
                extra_string = 'CONNECT %s:%s HTTP/1.0\r\n' % (self._real_host, self._real_port)
                extra_string += self._get_authentication_string() + '\r\n'
                self.send(extra_string)

                #expect a HTTP/1.0 200 Connection established
                response = self.response_class(self.sock, strict=self.strict, method=self._method)
                (version, code, message) = response._read_status()
                #probably here we can handle auth requests...
                if code != 200:
                        #proxy returned and error, abort connection, and raise exception
                        self.close()
                        raise socket.error, 'Proxy connection failed: %d %s' % (code, message.strip())
                #eat up header block from proxy....
                while True:
                        #should not use directly fp probablu
                        line = response.fp.readline()
                        if line == '\r\n': break
               
        def _get_authentication_string(self):
                auth_string = ''
                proxy_username = os.environ.get('proxy-username')
                proxy_password = os.environ.get('proxy-password')
                if proxy_username and proxy_password:
                        encoded_user_pass = base64.encodestring('%s:%s' % (proxy_username, proxy_password))
                        auth_string = 'Proxy-authorization: Basic %s\r\n' % encoded_user_pass
               
                return auth_string
class ProxyHTTPSConnection(ProxyHTTPConnection):
       
        default_port = 443

        def __init__(self, host, port = None, key_file = None, cert_file = None, strict = None):
                ProxyHTTPConnection.__init__(self, host, port)
                self.key_file = key_file
                self.cert_file = cert_file
        def connect(self):
                ProxyHTTPConnection.connect(self)
                #make the sock ssl-aware
                ssl = socket.ssl(self.sock, self.key_file, self.cert_file)
                self.sock = httplib.FakeSocket(self.sock, ssl)
class ConnectHTTPHandler(urllib2.HTTPHandler):


        def __init__(self, proxy=None, debuglevel=0):
                self.proxy = self._parse_proxy_details(proxy)
                urllib2.HTTPHandler.__init__(self, debuglevel)

        def do_open(self, http_class, req):
                if self.proxy is not None:
                        req.set_proxy(self.proxy, 'http')
                return urllib2.HTTPHandler.do_open(self, ProxyHTTPConnection, req)

        def _parse_proxy_details(self, proxy_string):
                proto, rest = urllib.splittype(proxy_string)
                host,  rest = urllib.splithost(rest)
                host,  port = urllib.splitport(host)
                return '%s:%s' % (host, port)
class ConnectHTTPSHandler(urllib2.HTTPSHandler):

        def __init__(self, proxy=None, debuglevel=0):
                self.proxy = self._parse_proxy_details(proxy)
                urllib2.HTTPSHandler.__init__(self, debuglevel)

        def do_open(self, http_class, req):
                if self.proxy is not None:
                        req.set_proxy(self.proxy, 'https')
                return urllib2.HTTPSHandler.do_open(self, ProxyHTTPSConnection, req)
       
        def _parse_proxy_details(self, proxy_string):
                proto, rest = urllib.splittype(proxy_string)
                host,  rest = urllib.splithost(rest)
                host,  port = urllib.splitport(host)
                return '%s:%s' % (host, port)  

SPREADSHEET_FORMATS=['csv','tsv','ods','tab','xls']
PRESENTATION_FORMATS=['ppt','pps']
DOCUMENT_FORMATS=['doc','odt','rtf','sxw','txt','htm','html']
mimes={'csv': 'text/csv', 
       'tsv': 'text/tab-separated-values',
       'tab': 'text/tab-separated-values',
       'doc': 'application/msword',
       'ods': 'application/x-vnd.oasis.opendocument.spreadsheet',
       'odt': 'application/vnd.oasis.opendocument.text',
       'rtf': 'application/rtf',
       'sxw': 'application/vnd.sun.xml.writer',
       'txt': 'text/plain',
       'xls': 'application/vnd.ms-excel',
       'ppt': 'application/vnd.ms-powerpoint',
       'pps': 'application/vnd.ms-powerpoint',
       'htm': 'text/html',
       'html': 'text/html'}
DOCUMENT_QUERY='http://docs.google.com/MiscCommands?command=saveasdoc&docID=%s&exportformat=%s'
SPREADSHEET_QUERY='http://spreadsheets.google.com/pub?key=%s&output=%s'
PRESENTATION_QUERY='https://docs.google.com/MiscCommands?command=saveasdoc&exportFormat=%s&docID=%s'


class sink:
    name='Google Docs'
    files=[]
    time=0
    _cookie_jar = None
    _url_google_auth          = 'https://www.google.com/accounts/ServiceLoginAuth'
    _url_google_followup  = 'https://docs.google.com'
    _user_agent = 'Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.8.1.6) Gecko/20061201 Firefox/2.0.0.6 (Ubuntu-feisty)'
    def __init__(self,config):
        try:
            import gdata.docs.service
        except(ImportError):
            self.installGoogleDocs()

        # Are any of these rare?
        import atom.service
        from urlparse import *
        import httplib
        import urllib2
        import urllib
        import os # dont like how it needs this...
        import cookielib

        print 'logging in to Google Docs'

        self._cookie_jar = cookielib.CookieJar()

        self.gd_client = gdata.docs.service.DocsService()
        self.gd_client.ClientLogin(config['username'],config['password'])
        
        login_data = {'PersistentCookies': 'true', 'Email': config['username'], 'Passwd': config['password'], 
                      'continue': self._url_google_followup, 'followup': self._url_google_followup}
        prepared_auth_url = self._url_google_auth
        response = self._open_https_url(prepared_auth_url, login_data)
        
        print 'logged in.'
        self.config=config
    def installGoogleDocs(self):
        import sys
        if sys.platform == 'linux2':
            import platform
            if platform.dist=='Ubuntu':
                import os
                os.system('gksu apt-get install python-gdata')
            else:
                print 'Please download and install http://gdata-python-client.googlecode.com/files/gdata.py-1.2.3.tar.gz'
        elif sys.platform == 'win32':
            print 'Please download and install http://gdata-python-client.googlecode.com/files/gdata.py-1.2.3.zip'
        elif sys.platform == 'mac':
            print 'Please download and install http://gdata-python-client.googlecode.com/files/gdata.py-1.2.3.zip'
    def getContent(self,file):
        return self.get_file(file['id'],file['format'])
    def get_files(self):
        try:
            files=[]
            for entry in self.gd_client.GetDocumentListFeed().entry:
                file={'format': self.config[entry.category[0].label],
                            'id': self.get_document_id(entry.GetAlternateLink().href),
                            #'author': {'name':entry.author[0].name.text,'email':entry.author[0].email.text},
                            #'updated': entry.updated.text,
                            #'published': entry.published.text,
                            'title': entry.title.text,
                            'edit-link': entry.GetEditLink().href,
                            'folder': ''}
                files.append(file)
            return files
        except(gdata.service.RequestError):
            print "Google Docs is not responding..."
            return self.get_files()
    def get_document_id(self, alternate_link):
        parsed_url = urlparse(alternate_link)
        url_params = parsed_url[4]
        document_id = url_params.split('=')[1]
        return document_id
    def get_file(self,id,format):
        if format in DOCUMENT_FORMATS or format in PRESENTATION_FORMATS:
            url=DOCUMENT_QUERY%(id,format)
        elif format in SPREADSHEET_FORMATS:
            url=SPREADSHEET_QUERY%(id,format)
        return self._open_https_url(url, post_data = None).read()
    def _open_https_url(self, target_url, post_data = None):
            # Opener will be assigned to either a proxy enabled or disabled opener
            opener    = None
            proxy_url = os.environ.get('http_proxy')
            
            if proxy_url:
                    opener = urllib2.build_opener(ConnectHTTPHandler(proxy = proxy_url),
                                                  ConnectHTTPSHandler(proxy = proxy_url),
                                                  urllib2.HTTPCookieProcessor(self._cookie_jar))
            else:
                    opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(self._cookie_jar))

            # Proxy or not, add the headers and place the POST reuqest
            opener.addheaders = [('User-agent', self._user_agent)]
            
            response = None
            if post_data:
                    response = opener.open(target_url, urllib.urlencode(post_data))
            else:
                    response = opener.open(target_url)
                    
            return response
    def create(self,file):
        try:
            ms = gdata.MediaSource(file['content'], mimes[file['format'].lower()], len(file['content']), '', file['title'])
        except(KeyError):
            print file['format']
            return
        
        media_entry = gdata.GDataEntry()
        media_entry.title = atom.Title(text=file['title'])

        if file['format'] in SPREADSHEET_FORMATS:
            media_entry.category.append(atom.Category(scheme=gdata.docs.service.DATA_KIND_SCHEME,
                                                      term=gdata.docs.service.SPREADSHEET_KIND_TERM))
        elif file['format'] in DOCUMENT_FORMATS:
            media_entry.category.append(atom.Category(scheme=gdata.docs.service.DATA_KIND_SCHEME,
                                    term=gdata.docs.service.DOCUMENT_KIND_TERM))
        elif file['format'] in PRESENTATION_FORMATS:
            media_entry.category.append(atom.Category(scheme=gdata.docs.service.DATA_KIND_SCHEME,
                                                      term=gdata.docs.service.PRESENTATION_KIND_TERM))
        #if file['starred']:
        #    if file['format'] in DOCUMENT_FORMATS:
        #        media_entry.category.append(atom.Category
        #                                (scheme='http://schemas.google.com/g/2005/labels',
        #                                 term='http://schemas.google.com/g/2005/labels#starred'))
        #    elif file['format'] in SPREADSHEET_FORMATS:
        #        media_entry.category.append(atom.Category
        #                                (scheme='http://schemas.google.com/docs/2007/folders/%s'%file['author']['email'],
        #                                 term='My Favorite Spreadsheets'))
        #if file['author']['name']:
        #    media_entry.author.name=file['author']['name']
        #if file['author']['email']:
        #    media_entry.author.email=file['author']['email']
        #if file['updated']:
        #    media_entry.id=file['updated']
        #if file['published']:
        #    media_entry.id=file['published']
        #if file['folder']:
        #    media_entry
        
        self.gd_client.Post(media_entry, '/feeds/documents/private/full',
                                media_source = ms,
                                extra_headers = {'Slug' : file['title'] })
    def remove(self,file):
        self.gd_client.Delete(file['edit-link'])>>>>>>> .r14
